package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertDataDto 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertDataDto alertDataDto) {
        //获取当前登录的用户id
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return alertDataMapper.selectAlertDataList(alertDataDto, userId);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public int handleAlertData(AlertData alertData) {

        //获取该报警数据发送给所有护工的ids
        AlertData data = selectAlertDataById(alertData.getId());
        //判空
        if (ObjectUtil.isEmpty(data)) {
            return 0;
        }
        LocalDateTime processingTime = alertData.getProcessingTime();
        //通过报警数据id获取创建时间
        Date createTime = data.getCreateTime();
        LocalDateTime createDateTime = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if (processingTime.isBefore(createDateTime.minusDays(1))) {
            throw new BaseException("处理时间不能早于报警时间");
        }
        ArrayList<Long> userIds = new ArrayList<>();
        List<AlertData> alertDataList = list(Wrappers.<AlertData>lambdaQuery().eq(AlertData::getIotId, data.getIotId())
                .eq(AlertData::getCreateTime, data.getCreateTime()));
        alertDataList.forEach(k -> {
            userIds.add(k.getUserId());
            k.setProcessingResult(alertData.getProcessingResult());
            k.setProcessingTime(alertData.getProcessingTime());
            k.setStatus(1);
            //获取当前登录的用户id和姓名
            k.setProcessorId(SecurityUtils.getLoginUser().getUser().getUserId());
            k.setProcessorName(SecurityUtils.getLoginUser().getUser().getUserName());
        });

        return updateBatchById(alertDataList) ? 1 : 0;
    }

}
